﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp.Study_DelegateEvent.EventDemo
{
    /// <summary>
    /// 事件发布者
    /// 事件的定义和调用，触发事件也可以写在里面
    /// </summary>
    public class Publisher
    {
        public event EventHandler<CustomEventArgs> CustomEvent;

        public class CustomEventArgs : EventArgs
        {
            public string OtherData { get; set; }
            public CustomEventArgs(string otherData)
            {
                OtherData = otherData;
            }
        }

        public void DoSomething(string otherData)
        {
            //调用事件之前可以做一些其它事情，业务处理...
            OnCustomEvent(new CustomEventArgs(otherData));
        }

        //在受保护的虚方法中包装事件的调用，这样就允许派生类重写调用行为
        protected virtual void OnCustomEvent(CustomEventArgs e)
        {
            CustomEvent?.Invoke(this, e);
        }
    }
}
